$(function() {
	var url = 'http://ittalentsapi.bashibozuk.eu/users/login?X-GameID=103';
	var data = window.sessionStorage.getItem('myData');
    if (data) {
        window.location = 'changepassword.html';
    };
	$('#login-button').click(function(event){
		event.preventDefault();
		var myData = {};
		myData.username = $('#username').val();
		myData.password = $('#password').val();
		$.ajax(url, {
			method: 'POST',
			cache:false,
			data : myData,
			crossDomain : true,
			success: function(data) {
				if(data.errors) {
					$('.errors').html('');
					for(i in data.errors) {
						$('.errors').html(data.errors[i].join('<br>'));
					}
				}
				else {
					window.sessionStorage.setItem('myData', JSON.stringify(data));
					window.location = 'play.html';
				}
			}
		});		
	});
	$('#register-button').click(function(event){
		event.preventDefault();
		window.location = 'register.html';
	});
});